<?php

namespace app\agent\model\agent;

use app\agent\model\BaseModel;
use app\agent\model\agent\User as AgentUser;
use think\Db;
use think\Session;

/**
 * 代理商模型
 */
class Agent extends BaseModel{
    protected $name = 'agent';
    public function add($data){
        $this->allowField(true)->save($data);
        return $this->agent_id;
    }

    public function upd($data,$agentId){
        return $this->allowField(true)->save($data,['agent_id'=>$agentId]);
    }
    /**
     * 获取单个代理信息
     */
    public function getAgentInfo($agentId){
        return Db::name('agent')
            ->where('agent_id', '=', $agentId)
            ->find();
    }
    /**
     * 获取代理商信息
     * @param int|null $agentId
     * @throws \think\exception\DbException
     */
    public static function detail($agentId){
        return Db::name('agent')
            ->where('agent_id', '=', $agentId)
            ->order(['create_time' => 'desc'])
            ->paginate(10, false, [
                'query' => request()->request()
            ]);
        return static::get($agentId ?: []);
    }

    /**
     * 获取代理商列表
     * @param boolean $is_recycle
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($is_recycle = false){
        return Db::name('agent')
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate(10, false, [
                'query' => request()->request()
            ]);
    }


    /**
     * 获取代理商合作伙伴列表
     */
    public function getPartnerList($agent_id,$type=0){
        //获取当前登录用户的所有下级  带类型
        $session = Session::get('yoshop_agent');
        if($session['agent']['type']==0){
            $info=Db::name('agent_user')->where(['pinpai_id'=>$session['agent']['pinpai_id']])->select();
            $resNext=$this->get_all_child2($info->toArray(),$session['agent']['agent_id'],$type);
        }else{
            $resNext=[];
            $info=Db::name('agent_user')->where(['recommend_id'=>$session['agent']['agent_id'],'type'=>$type])->select();
            if($info){
                foreach ($info as $k=>$v){
                    $resNext[]=$v['agent_user_id'];
                }
            }
            $resNext[]=$session['agent']['agent_id'];
        }

        $resturn=Db::name('agent')
            ->alias('a')
            ->join('agent_user u','a.recommend_id = u.agent_user_id')
            ->where('a.agent_id', 'in', $resNext)
            ->where('a.type', '=', $type)
            //->where('is_delete', '=', 0)
            ->order(['a.create_time' => 'desc'])
            ->field('a.*,u.real_name as recommend_realname,u.user_name as recommend_username,u.type as recommend_typename')
            ->paginate(10, false, [
                'query' => request()->request()
            ]);
        //var_dump($resturn->toArray());
        return $resturn;
    }
    public function getperList($agent_id,$type=0){
        //获取当前登录用户的所有下级  带类型
        $resturn=Db::name('agent')
            ->alias('a')
            ->join('agent_user u','a.recommend_id = u.agent_user_id')
            ->where('a.recommend_id', '=', $agent_id)
            ->where('a.type', '=', $type)
            //->where('is_delete', '=', 0)
            ->order(['a.create_time' => 'desc'])
            ->field('a.*,u.real_name as recommend_realname,u.user_name as recommend_username,u.type as recommend_typename')
            ->paginate(10, false, [
                'query' => request()->request()
            ]);
        //var_dump($resturn->toArray());
        return $resturn;
    }

    //递归获取所有的子分类的ID(带类型)
    private function get_all_child2($array,$id,$type){
        $arr = array();
        foreach($array as $v){
            if($v['recommend_id'] == $id ){
                if($v['type']==$type){
                    $arr[] = $v['agent_id'];
                }
                $arr = array_merge($arr,$this->get_all_child2($array,$v['agent_id'],$type));
            };
        };
        return $arr;
    }
    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        User::withTrashed()->select();
        return $this->transaction(function () {
            // 删除商家用户信息
            (new AgentUser)->setDelete($this['agent_id']);
            // 设置当前商城为已删除
            return $this->save(['is_delete' => 1]);
        });
    }
}
